package com.huopaolan.yohuoshop.Mvp.Presenter;

import android.util.Log;

import com.huopaolan.lib_core.Mvp.Presenter.BasePresenter;
import com.huopaolan.yohuoshop.Bean.BrandListEntity;
import com.huopaolan.yohuoshop.Bean.ShoesEntity;
import com.huopaolan.yohuoshop.Mvp.Contract.ShoesFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ShoesFragmentPresenter extends BasePresenter<ShoesFragmentContract.ShoesIView, ShoesFragmentContract.ShoesIModel> {
    protected static int page = 1;

    @Inject
    public ShoesFragmentPresenter(ShoesFragmentContract.ShoesIView shoesIView, ShoesFragmentContract.ShoesIModel shoesIModel) {
        super(shoesIView, shoesIModel);
    }

    /**
     * 请求足球数据
     */
    public void requestShoes() {
        Log.d("requestShoes", "请求成功");
        JSONObject shoes = new JSONObject();
        try {
            shoes.put("page", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = shoes.toString();
        m.requestShoes(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ShoesEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ShoesEntity entity) {
                        Log.d("onNext", "请求中...");
                        buildData(entity);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void buildData(ShoesEntity entity) {
        //banner集合
        List<ShoesEntity.Banner> bannerList = buildBanner(entity);
        //brand集合
        List<ShoesEntity.Brand> brandList = buildBrand(entity);
        //values集合
        List<ShoesEntity.Values> valuesList = buildValues(entity);
        Map<String, List> map = new HashMap<>();
        map.put("bannerList", bannerList);
        map.put("brandList", brandList);
        map.put("valuesList", valuesList);
        v.shoesSuccess(map);
    }

    private List<ShoesEntity.Values> buildValues(ShoesEntity entity) {
        List<ShoesEntity.Values> values = new ArrayList<>();
        for (int i = 0; i < entity.values.size(); i++) {
            if (Integer.parseInt(entity.values.get(i).goods_id) >= 1 && Integer.parseInt(entity.values.get(i).goods_id) <= 10) {
                ShoesEntity.Values valueslist = new ShoesEntity.Values();
                valueslist.brand_id = entity.values.get(i).brand_id;
                valueslist.category_id = entity.values.get(i).category_id;
                valueslist.detail_flag = entity.values.get(i).detail_flag;
                valueslist.discount_detail = entity.values.get(i).discount_detail;
                valueslist.follow = entity.values.get(i).follow;
                valueslist.goods_discount_price = entity.values.get(i).goods_discount_price;
                valueslist.goods_guarantee = entity.values.get(i).goods_guarantee;
                valueslist.goods_id = entity.values.get(i).goods_id;
                valueslist.goods_img_path = entity.values.get(i).goods_img_path;
                valueslist.goods_name = entity.values.get(i).goods_name;
                valueslist.goods_original_price = entity.values.get(i).goods_original_price;
                valueslist.goods_popularity = entity.values.get(i).goods_popularity;
                valueslist.shop_id = entity.values.get(i).shop_id;
                valueslist.time = entity.values.get(i).time;
                values.add(valueslist);
            }
        }
        return values;
    }

    private List<ShoesEntity.Brand> buildBrand(ShoesEntity entity) {
        List<ShoesEntity.Brand> brands = new ArrayList<>();
        for (int i = 0; i < entity.brand.size(); i++) {
            if (Integer.parseInt(entity.brand.get(i).brand_id) >= 1 && Integer.parseInt(entity.brand.get(i).brand_id) <= 8) {
                ShoesEntity.Brand brand = new ShoesEntity.Brand();
                brand.brand_name = entity.brand.get(i).brand_name;
                brand.brand_icon = entity.brand.get(i).brand_icon;
                brand.brand_id = entity.brand.get(i).brand_id;
                brand.menu_id = entity.brand.get(i).menu_id;
                brands.add(brand);
            }
        }
        return brands
                ;
    }

    private List<ShoesEntity.Banner> buildBanner(ShoesEntity entity) {
        List<ShoesEntity.Banner> banners = new ArrayList<>();
        for (int i = 0; i < entity.banner.size(); i++) {
            if (Integer.parseInt(entity.banner.get(i).recommend_id) >= 1 && Integer.parseInt(entity.banner.get(i).recommend_id) <= 7) {
                ShoesEntity.Banner banner = new ShoesEntity.Banner();
                banner.recommend_id = entity.banner.get(i).recommend_id;
                banner.recommend_name = entity.banner.get(i).recommend_name;
                banner.recommend_path = entity.banner.get(i).recommend_path;
                banner.recommend_url = entity.banner.get(i).recommend_url;
                banners.add(banner);
            }
        }
        return banners;
    }
}
